# Asterisk events handlers
# Support Asterisk >= 1.4

import sys
import logging
import asterisk.manager

from astpychannel import Channel
from astpydb import PersistData

def handle_agents_event (event, manager):
	""" Handles asterisk 'Agents' event """
	agent = event.get_header("Agent")
	status = event.get_header("Status")
	if status == "AGENT_LOGGEDOFF":
		status = "loggedoff"
		channel = ""
	else:
		status = "loggedin"
		channel = event.get_header("LoggedInChan")
	db = PersistData()
	if not db.agentstatus(agent, channel, status):
		logging.error("Error persisting agent status (%s,%s,%s)" % (agent, channel, status))

def handle_agentlogin_event (event, manager):
	""" Handles asterisk 'AgentLogin' events """
	agent = event.get_header("Agent")
	channel = event.get_header("Channel")
	db = PersistData()
	if not db.agentstatus(agent, channel, "loggedin"):
		logging.error("Error persisting agent status (%s,%s,%s)" % (agent, channel, status))

def handle_agentlogoff_event (event, manager):
	""" Handles asterisk 'AgentLogoff' events """
	agent = event.get_header("Agent")
	db = PersistData()
	if not db.agentstatus(agent, "", "loggedoff"):
		logging.error("Error persisting agent status (%s,%s)" % (agent, status))

def handle_peerentry_event (event, manager):
	""" Handles asterisk 'PeerEntry' event for IAX and SIP """
	tech = event.get_header("Channeltype")
	channel = event.get_header("ObjectName")
	status = event.get_header("Status")
	db = PersistData()
	if not db.peerstatus(tech, channel, status):
		logging.error("Error persisting peer status (%s, %s, %s)" % (tech, channel, status))

def handle_peerstatus_event (event, manager):
	""" Handles asterisk 'PeerStatus' event for IAX and SIP """
	# Peer is coming with the technology SIP/test or IAX/test
	peerfull = event.get_header("Peer")
	peer = peerfull[peerfull.find("/") + 1:]
	try:
		tech = event.get_header("ChannelType")
	except KeyError:
		# Missing ChannelType on unregister event, so get it from
		# the Peer header (Fixed in bugs.digium.com [0012904]).
		tech = peerfull[:peerfull.find("/")]
		
	status = event.get_header("PeerStatus")
	db = PersistData()
	if not db.peerstatus(tech, peer, status):
		logging.error("Error persisting peer status (%s, %s, %s, %s)" % (tech, peer, status))

def handle_link_event (event, manager):
	""" Handles asterisk 'Link' event """
	source = Channel(event.get_header("Channel1"))
	destination = Channel(event.get_header("Channel2"))
	logging.debug("Event: Link, received (%s, %s)" % (source.getUniqueChannel(), destination.getUniqueChannel()))
	db = PersistData()
	if not db.oncall(source.getUniqueChannel(), destination.getUniqueChannel()):
		logging.error("Error persisting OnCall(%s, %s)" % (source.getUniqueChannel(), destination.getUniqueChannel()))

def handle_hangup_event (event, manager):
	""" Handles asterisk 'Hangup' event """
	channel = Channel(event.get_header("Channel"))
	logging.debug("Event: Hangup, received (%s)" % channel.getUniqueChannel())
	db = PersistData()
	if not db.hangup(channel.getUniqueChannel()):
		logging.error("Error persisting Hangup(%s)" % channel.getUniqueChannel())

def handle_dial_event (event, manager):
	""" Handles asterisk 'Dial' event """
	source = Channel(event.get_header("Source"))
	destination = Channel(event.get_header("Destination"))
	logging.debug("Event: Dial, received (%s, %s)" % (source.getUniqueChannel(), destination.getUniqueChannel()))
	db = PersistData()
	if not db.ringing(source.getUniqueChannel(), destination.getUniqueChannel()):
		logging.error("Error persisting Ringing(%s, %s)" % (source.getUniqueChannel(), destination.getUniqueChannel()))

